package com.springcloud.zuul.common.aspect;

import com.google.gson.Gson;
import com.springcloud.zuul.common.annotation.SysLog;
import com.springcloud.zuul.common.utlis.ConvertDateTime;
import com.springcloud.zuul.common.utlis.ExceptionLogUtil;
import com.springcloud.zuul.common.utlis.HttpContextUtils;
import com.springcloud.zuul.common.utlis.IPUtils;
import com.springcloud.zuul.entity.SysLogEntity;
import com.springcloud.zuul.entity.SysUserEntity;
import com.springcloud.zuul.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.NotReadablePropertyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;


/**
 * 系统日志，切面处理类
 *
 */
@Aspect
@Component
@Slf4j
public class SysLogAspect {
	@Autowired
	private SysLogService sysLogService;

	@Pointcut("@annotation(com.springcloud.zuul.common.annotation.SysLog)")
	public void logPointCut() {

	}

	@Around("logPointCut()")
	public Object around(ProceedingJoinPoint point) throws Throwable {
		long beginTime = System.currentTimeMillis();
		//执行方法
		Object result = point.proceed();
		//执行时长(毫秒)
		long time = System.currentTimeMillis() - beginTime;

		//保存日志
		saveSysLog(point, time);

		return result;
	}

	private void saveSysLog(ProceedingJoinPoint joinPoint, long time) {
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod();

		SysLogEntity sysLog = new SysLogEntity();
		//日志编码
		sysLog.setLogId(RandomStringUtils.randomAlphanumeric(20));
		SysLog syslog = method.getAnnotation(SysLog.class);
		if(syslog != null){
			//注解上的描述，标题
			sysLog.setTitle(syslog.value());
			//日志类型
			sysLog.setType(syslog.type());
			switch (syslog.type()){
				//日志类型  1.人为操作
				case 1:
					//获取用户
					SysUserEntity user = ((SysUserEntity) SecurityUtils.getSubject().getPrincipal());
					//获取用户编码
					sysLog.setUserId(user.getUserId());
					//获取用户名称
					sysLog.setUserName(user.getUserName());
					break;
				//日志类型  2.脚本操作
				case 2:
					sysLog.setUserName("脚本操作");
					break;
				default:
					break;
			}
		}
		//获取request
		HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
		//请求URL
		sysLog.setRequestUri(request.getRequestURI());
		//请求方式
		sysLog.setMethod(request.getMethod());
		//请求的参数
		Object[] args = joinPoint.getArgs();
		try{
			String params = new Gson().toJson(args);
			sysLog.setParams(params);
		}catch (Exception e){
			log.error(ExceptionLogUtil.getStackTrace(e));
		}
		//设置IP地址
		sysLog.setRemoteAddr(IPUtils.getIpAddr(request));
		sysLog.setTime(time);
		sysLog.setInstm(ConvertDateTime.getCurrentTime());
		//保存系统日志
		sysLogService.save(sysLog);
	}
}
